package com.example.redis.rest;

import com.example.redis.service.AsyncService;
import com.example.redis.service.FastRedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.redis.util.RedisLockRegistry;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;

/**
 * @author void
 * @date 2020/10/19 18:50
 * @desc
 */
@Slf4j
@RestController
public class CacheController {

    @Autowired
    private AsyncService asyncService;
    @Autowired
    private FastRedisService fastRedisService;

    @GetMapping("/asyncLockTest")
    public String asyncLockTest(){
        String result = "FAIL";
        try{
            asyncService.doLock();
        }catch (Exception e){
            e.printStackTrace();
            return result;
        }
        return "SUCCESS";
    }
    
    @GetMapping("/test2")
    public String test2(){
        ExecutorService executor = Executors.newSingleThreadExecutor();
        executor.submit(()->{
            asyncService.doLock();
        });
        executor.shutdown();
        return "SUCCESS";
    }

    @GetMapping("/setValue")
    public void setValue(String str){
        fastRedisService.set("TEST_STR", str);
    }
}
